Skip to content

gh-152305: Fix pure-Python time.strftime AttributeError on %Y/%G/%C/%F#152306

Merged
StanFromIreland merged 3 commits into
python:mainfrom
tonghuaroot:fix-gh-152305-strftime-time-year
Jun 27, 2026
Merged

gh-152305: Fix pure-Python time.strftime AttributeError on %Y/%G/%C/%F#152306
StanFromIreland merged 3 commits into
python:mainfrom
tonghuaroot:fix-gh-152305-strftime-time-year

Conversation

@tonghuaroot

@tonghuaroot tonghuaroot commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

_wrap_strftime in the pure-Python datetime implementation reads
object.year in its year-normalization branch. When strftime is called on a
datetime.time (which has no .year), this raises
AttributeError: 'time' object has no attribute 'year' for the %Y, %G, %C
and %F directives. The crash happens in the branch guard itself, before the
_need_normalize_century() platform check, so it occurs on every platform.

The C accelerator is correct: a time has no date, so strftime fills the
1900-01-01 placeholder and returns '1900', '19', '1900-01-01' and '1900'
respectively.

The year being formatted is timetuple[0], which is 1900 for a time and
object.year for a date/datetime. This replaces object.year with
timetuple[0] at both occurrences, fixing time and leaving date/datetime
behaviour unchanged: the new pure-Python output equals the long-standing C
accelerator output.

Tests in TestTime.test_strftime assert the four directives, and run under both
the C and pure implementations via the test_datetime parametrization.

Comment thread Lib/_pydatetime.py Outdated
Comment thread Misc/NEWS.d/next/Library/2026-06-26-15-41-34.gh-issue-152305.WnbbBc.rst Outdated
@StanFromIreland StanFromIreland merged commit 5733361 into python:main Jun 27, 2026
55 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @tonghuaroot for the PR, and @StanFromIreland for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14, 3.15.
🐍🍒⛏🤖

@bedevere-app

bedevere-app Bot commented Jun 27, 2026

Copy link
Copy Markdown

GH-152425 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Jun 27, 2026
@bedevere-app

bedevere-app Bot commented Jun 27, 2026

Copy link
Copy Markdown

GH-152426 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Jun 27, 2026
StanFromIreland added a commit that referenced this pull request Jun 27, 2026
…irectives (GH-152306) (#152426)

(cherry picked from commit 5733361)

Co-authored-by: tonghuaroot (童话) <tonghuaroot@gmail.com>
Co-authored-by: Stan Ulbrych <stan@python.org>
StanFromIreland added a commit that referenced this pull request Jun 27, 2026
…irectives (GH-152306) (#152425)

(cherry picked from commit 5733361)

Co-authored-by: tonghuaroot (童话) <tonghuaroot@gmail.com>
Co-authored-by: Stan Ulbrych <stan@python.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants